﻿using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Custom;

namespace CoreTypes
{
   public class PolicyRuleHelper
   {
      public PolicyRule Rule { get; set; }

      public string Caption
      {
         get
         {
            switch (Rule)
            {
               case PolicyRule.Allow: return "Allow";
               case PolicyRule.Block: return "Block";
               case PolicyRule.Deny: return "Deny";
               case PolicyRule.Ignore: return "Ignore";
               case PolicyRule.Ask: return "Ask Me";
            }
            return "";
         }
      }

      public PolicyRuleHelper(PolicyRule rule)
      {
         Rule = rule;
      }
   }

   public class PrivacyRuleHelper
   {
      public PrivacyRule Rule { get; set; }

      public string Caption
      {
         get
         {
            switch (Rule)
            {
               case PrivacyRule.Anyone: return "Anyone";
               case PrivacyRule.MyContats: return "My Contats Only";
               case PrivacyRule.VipContacts: return "Vip Contacts Only";
               case PrivacyRule.Nobody: return "Nobody";
            }
            return "";
         }
      }

      public PrivacyRuleHelper(PrivacyRule rule)
      {
         Rule = rule;
      }
   }

   public static class XElementWrapHelper
   {
      public static void ClosePreviosOpenedXmlDocument()
      {
         XElement.CloseCurrentDocument();
      }
   }

   [DataContract(Namespace = "CoreTypes")]
   public struct AccountSettings
   {
      [DataMember]
      public int LogonDelay;
      [DataMember]
      public int NATimeout;
      [DataMember]
      public int AwayTimeout;
      [DataMember]
      public StatusCodes StartupStatus;
      [DataMember]
      public PolicyRule MyContactMessageRule;
      [DataMember]
      public PolicyRule MyContactInvitationRule;
      [DataMember]
      public PolicyRule VipContactInvitationRule;
      [DataMember]
      public PolicyRule UnknownUserMessageRule;
      [DataMember]
      public PolicyRule UnknownUserInvitationRule;
   }

   [DataContract(Namespace = "CoreTypes")]
   public struct ContactSettings
   {
      [DataMember]
      public bool? NewWindow;
      [DataMember]
      public bool? P2PAllowed;
      [DataMember]
      public Color? TextColor;
      [DataMember]
      public Color? NameColor;
      [DataMember]
      public bool? CheckSlang;
      [DataMember]
      public bool? SaveHistory;
      [DataMember]
      public PolicyRule MessageRule;
      [DataMember]
      public PolicyRule InvitationRule;
      [DataMember]
      public PolicyRule VisibilityRule;

      public override bool Equals(object obj)
      {
         ContactSettings b = (ContactSettings)obj;
         return
            NewWindow == b.NewWindow &&
            P2PAllowed == b.P2PAllowed &&
            CheckSlang == b.CheckSlang &&
            SaveHistory == b.SaveHistory &&
            MessageRule == b.MessageRule &&
            InvitationRule == b.InvitationRule &&
            VisibilityRule == b.VisibilityRule;
      }
      public static bool operator ==(ContactSettings a, ContactSettings b)
      {
         return a.Equals(b);
      }
      public static bool operator !=(ContactSettings a, ContactSettings b)
      {
         return !a.Equals(b);
      }
   }

   [DataContract(Namespace = "CoreTypes")]
   public class ServiceSession
   {
      [DataMember]
      public int Port { get; set; }
      [DataMember]
      public string Host { get; set; }
      [DataMember]
      public decimal Token { get; set; }
      [DataMember]
      public string Secret { get; set; }

      public bool KeepConnection { get; set; }
      public ServiceNodeType NodeType { get; set; }
      public ICommunicationServiceObject Service { get; set; }

      public ServiceSession(string host, int port, decimal token, string secret)
      {
         Host = host;
         Port = port;
         Token = token;
         Secret = secret;
         KeepConnection = true;
      }
   }

   [DataContract(Namespace = "CoreTypes")]
   public class RegistrationInfo
   {
      [DataMember]
      public long RegId { get; set; }
      [DataMember]
      public string Language { get; set; }
      [DataMember]
      public string Nickname { get; set; }
      [DataMember]
      public string LoginEmail { get; set; }
      [DataMember]
      public string DesiredLogin { get; set; }
      [DataMember]
      public Image HumanCheckImage { get; set; }
      [DataMember]
      public string HumanCheckCode { get; set; }
      [DataMember]
      public string DesiredPassword { get; set; }

      public ServiceConfig ServiceConfig { get; set; }

      public RegistrationInfo()
      {
      }
   }

   [DataContract(Namespace = "CoreTypes")]
   public class ServiceEntrance
   {
      [DataMember]
      public string Code;
      [DataMember]
      public string Addr;
      [DataMember]
      public string Host;
      [DataMember]
      public ushort Port;
      [DataMember]
      public string Desc;
      [DataMember]
      public string EntranceName;
      [DataMember]
      public ServerType ServerType;
      [DataMember]
      public Collection<ServiceRole> Roles;

      public short FailsCount;

      public ServiceEntrance(string code, ushort port, ServerType type,
                  string host, string name, string desc, bool readOnly)
      {
         Code = code;
         Port = port;
         Host = host;
         Desc = desc;
         FailsCount = 0;
         Addr = "0.0.0.0";
         ServerType = type;
         EntranceName = name;
      }
   }
}